<?php
declare (strict_types = 1);

namespace app\admin\controller;

use app\admin\BaseController;
use app\admin\model\AdminGroup;
use app\admin\validate\GroupCheck;
use think\exception\ValidateException;
use think\facade\Db;
use think\facade\View;

class Role extends BaseController
{
    public function index()
    {
        return view();
    }

    //管理员列表
    public function get_list()
    {
        $param = get_params();
        $where = array();
        if (!empty($param['keywords'])) {
            $where[] = ['id|title|desc','like', '%' . $param['keywords'] . '%'];
        }
        $rows = empty($param['limit']) ? get_config(app.page_size) : $param['limit'];
        $group = AdminGroup::where($where)
            ->order('create_time asc')
            ->paginate($rows, false, ['query' => $param]);
        return table_assign(1, '', $group);
    }

    //添加&编辑
    public function add()
    {
        $id = empty(get_params('id')) ? 0 : get_params('id');
        if ($id > 0) {
            $role = Db::name('admin_group')->where(['id' => $id])->find();
            View::assign('role', $role);
        }
        View::assign('id', $id);
        return view();
    }

    //提交保存
    public function post_submit()
    {
        if (request()->isAjax()) {
            $param = get_params();

            if (!empty($param['id']) && $param['id'] > 0) {
                try {
                    validate(GroupCheck::class)->scene('edit')->check($param);
                } catch (ValidateException $e) {
                    // 验证失败 输出错误信息
                    return to_assign(0, $e->getError());
                }
                //为了系统安全id为1的系统所有者管理组不允许修改
                if ($param['id'] == 1) {
                    return to_assign(0, '为了系统安全,该管理组不允许修改');
                }
                /*
                if (!empty($param['rules'])) {
                    $param['rules'] = implode(',', $param['rules']);
                }
                if (!empty($param['menus'])) {
                    $param['menus'] = implode(',', $param['menus']);
                }
                */
                Db::name('admin_group')->where(['id' => $param['id']])->strict(false)->field(true)->update($param);
                add_log('edit',$param['id'],$param);
            } else {
                try {
                    validate(GroupCheck::class)->scene('add')->check($param);
                } catch (ValidateException $e) {
                    // 验证失败 输出错误信息
                    return to_assign(0, $e->getError());
                }
                /*
                if (!empty($param['rules'])) {
                    $param['rules'] = implode(',', $param['rules']);
                }
                if (!empty($param['menus'])) {
                    $param['menus'] = implode(',', $param['menus']);
                }
                */
                $gid = Db::name('admin_group')->strict(false)->field(true)->insertGetId($param);
                add_log('add',$gid,$param);
            }
            //清除菜单\权限缓存
            clear_cache('adminMenu');
            clear_cache('adminRules');
            return to_assign();
        }
    }

    //删除
    public function delete()
    {
        $id = get_params("id");
        if ($id == 1) {
            return to_assign(0, "该组是系统所有者，无法删除");
        }
        if (Db::name('AdminGroup')->delete($id) !== false) {
            add_log('delete',$id,[]);
            return to_assign(1, "删除角色成功");
        } else {
            return to_assign(0, "删除失败");
        }
    }
}
